<?php
if ( !defined('IN_ADMINCP') || !defined('IN_SEC') ){
	exit('Access Denied.');
}

$id = abs(intval($_G['gp_id']));

if( $id && !$team = fetch('team_task', $id) ){
	$admincp->cpmsg('任务不存在');
}

$teams = limitquery('team', array('adminid' => $admincp->adminid));

if( is_post() ){
	if( !confirm_password($admincp->admin) ){
		$admincp->cpmsg('密码不正确！授权操作失败！');
	}
	$name = $_G['gp_name'] ? htmlspecialchars($_G['gp_name']) : $admincp->cpmsg('请填写项目名称');
	$teamid = abs(intval($_G['gp_teamid']));
	$teamid = $teamid ? $teamid : $admincp->cpmsg('请选择项目');

	$begin_time = $_G['gp_begin_time'] ? strtotime($_G['gp_begin_time']) : $admincp->cpmsg('请填写开始时间');
	$end_time = $_G['gp_end_time'] ? strtotime($_G['gp_end_time']) : $admincp->cpmsg('请填写结束时间');
	if( $end_time <= ($begin_time - 86400) ){
		$admincp->cpmsg('结束时间不能大于开始时间');
	}

	$taskid = $task['id'];
	$update = array(
		'name' => $name,
		'teamid' => $teamid,
		'begin_time' => $begin_time,
		'end_time' => $end_time
	);
	if( $team ){
		update('team_task', $team['id'], $update);
		$admincp->log('team_task', $taskid, '编辑任务：' . $name, $update);
	}else{
		$update['adminid'] = $admincp->adminid;
		$update['create_time'] = SYS_TIME;
		$teamid = DB::Insert('team_task', $update);
		$admincp->log('team_task', $taskid, '添加任务：' . $name, $update);
	}
	/*
	foreach( $members as $k => $v ){
		if( !$team['members'][$k] ){
			DB::Insert('team_member', array(
				'teamid' => $teamid,
				'adminid' => $k,
				'create_time' => SYS_TIME
			));
		}
	}
	*/
	$admincp->success();
}
?>